home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / dev / e / amigae33a.lha / E_v3.3a / Src.lha / Src / OOmodules / library / locale.e < prev    next >
Text File  |  1996-09-21  |  8KB  |  393 lines

  1. OPT MODULE
  2.  
  3. MODULE  'oomodules/library',
  4.         'oomodules/object',
  5.         'oomodules/sort/string',
  6.  
  7.         'locale',
  8.         'libraries/locale'
  9.  
  10. EXPORT OBJECT locale OF library
  11. /****** library/locale ******************************
  12.  
  13.     NAME
  14.         locale of library
  15.  
  16.     PURPOSE
  17.         Object wrapper for the locale.library
  18.  
  19.     ATTRIBUTES
  20.         catalog:LONG -- Catalog as returned from OpenCatalog()
  21.  
  22.         builtinLanguage:LONG -- String that says which language we want.
  23.             NIL is for english.
  24.  
  25.     SEE ALSO
  26.         library
  27.  
  28. ********/
  29.   catalog -> the current catalog
  30.   builtinLanguage -> if NIL it's english
  31. ENDOBJECT
  32.  
  33. EXPORT PROC init() OF locale
  34. /****** locale/init ******************************
  35.  
  36.     NAME
  37.         init() of locale -- Initilization of the object.
  38.  
  39.     SYNOPSIS
  40.         locale.init()
  41.  
  42.     FUNCTION
  43.         Opens any version of the library.
  44.  
  45.     SEE ALSO
  46.         locale
  47.  
  48. ********/
  49.  
  50.   self.version:=0
  51.   self.open()
  52.  
  53. ENDPROC
  54.  
  55. PROC open() OF locale
  56. /****** locale/open ******************************
  57.  
  58.     NAME
  59.         open() of locale -- Open locale.
  60.  
  61.     SYNOPSIS
  62.         locale.open()
  63.  
  64.     FUNCTION
  65.         Opens the locale.library and throws an exceptions if opening failed.
  66.  
  67.     SEE ALSO
  68.         locale, library/open
  69.  
  70. ********/
  71.  
  72.   IF localebase THEN RETURN
  73.  
  74.   IF (localebase := OpenLibrary('locale.library', self.version))=NIL THEN Throw("lib", 'Unable to open locale.library')
  75.  
  76. ENDPROC
  77.  
  78. PROC close() OF locale
  79. /****** locale/close ******************************
  80.  
  81.     NAME
  82.         close() of locale -- Close locale.library
  83.  
  84.     SYNOPSIS
  85.         locale.close()
  86.  
  87.     FUNCTION
  88.         Closes the library if it was open.
  89.  
  90.     SEE ALSO
  91.         locale, library/close
  92.  
  93. ********/
  94.  
  95.   IF localebase THEN CloseLibrary(localebase)
  96.  
  97. ENDPROC
  98.  
  99. PROC openCatalog(locale, name:PTR TO CHAR, builtinLanguage=NIL:PTR TO CHAR) OF locale
  100. /****** locale/openCatalog ******************************
  101.  
  102.     NAME
  103.         openCatalog() of locale -- Open catalog.
  104.  
  105.     SYNOPSIS
  106.         locale.openCatalog(LONG, PTR TO CHAR, PTR TO CHAR=NIL:PTR TO CHAR)
  107.  
  108.         locale.openCatalog(locale, name, builtinLanguage)
  109.  
  110.     FUNCTION
  111.         Opens the specified catalog. If the catalog of this object is open
  112.         when you call this function it is closed first.
  113.  
  114.     INPUTS
  115.         locale:LONG -- Pointer to locale structure, normally left NIL.
  116.  
  117.         name:PTR TO CHAR -- Name o catalog to open.
  118.  
  119.         builtinLanguage:PTR TO CHAR -- 'Native' language of your program.
  120.             Used if the desired catalog couldn't be opened. If NIL, your
  121.             program speaks english.
  122.  
  123.     RESULT
  124.         LONG -- Opended catalog or NIL if opening failed.
  125.  
  126.     EXAMPLE
  127.        /*
  128.         * Try to open term's catalog.
  129.         */
  130.  
  131.         NEW locale.new(["ctlg", 'term.catalog'])
  132.  
  133.  
  134.  
  135.        /*
  136.         * Success?
  137.         */
  138.  
  139.         IF locale.catalog
  140.  
  141.          /*
  142.           * do anything with it
  143.           */
  144.  
  145.         ENDIF
  146.  
  147.  
  148.  
  149.        /*
  150.         * Try to open the Prefs catalog. If term.catalog is still open,
  151.         * it will be closed first.
  152.         */
  153.  
  154.         locale.openCatalog(NIL, 'prefs.catalog', NIL)
  155.  
  156.        /*
  157.         * do anything with it
  158.         */
  159.  
  160.     SEE ALSO
  161.         locale
  162.  
  163. ********/
  164.  
  165.   IF self.catalog -> if there's a catalog open
  166.  
  167.     self.closeCatalog(self.catalog)
  168.  
  169.     self.catalog := NIL
  170.  
  171.   ENDIF
  172.  
  173.   self.builtinLanguage := builtinLanguage
  174.  
  175.   self.catalog := OpenCatalogA(locale,name, IF self.builtinLanguage=NIL THEN 'english' ELSE self.builtinLanguage)
  176.  
  177.   RETURN self.catalog
  178.  
  179. ENDPROC
  180.  
  181. PROC closeCatalog(catalog) OF locale
  182. /****** locale/closeCatalog ******************************
  183.  
  184.     NAME
  185.         closeCatalog() of locale -- Close catalog.
  186.  
  187.     SYNOPSIS
  188.         locale.closeCatalog(LONG)
  189.  
  190.         locale.closeCatalog(catalog)
  191.  
  192.     FUNCTION
  193.         Closes a catalog that was opened with openCatalog().
  194.  
  195.     INPUTS
  196.         catalog:LONG -- Catalog to close.
  197.  
  198.     SEE ALSO
  199.         locale
  200.  
  201. ********/
  202.  
  203.   IF catalog THEN CloseCatalog(catalog)
  204.  
  205. ENDPROC
  206.  
  207. PROC getString(number, default=NIL:PTR TO CHAR) OF locale
  208. /****** locale/getString ******************************
  209.  
  210.     NAME
  211.         getString() of locale -- Get string from open catalog.
  212.  
  213.     SYNOPSIS
  214.         locale.getString(LONG, PTR TO CHAR=NIL:PTR TO CHAR)
  215.  
  216.         locale.getString(number, default)
  217.  
  218.     FUNCTION
  219.         Gets a string from an opened catalog and returns it. If it fails,
  220.         the passed default string will be returned.
  221.  
  222.     INPUTS
  223.         number:LONG -- Number of string to get from the catalog.
  224.  
  225.         default:PTR TO CHAR -- Default string to return on failure.
  226.  
  227.     RESULT
  228.         PTR TO CHAR -- string from catalog or default string.
  229.         LONG -- "NoCa" if no catalog is open.
  230.  
  231.     SEE ALSO
  232.         locale
  233.  
  234. ********/
  235. -> gets a string from the current catalog
  236. -> if no catalog is open returns "NoCa" (no catalog)
  237.  
  238.   IF self.catalog THEN RETURN GetCatalogStr(self.catalog,number,default) ELSE RETURN "NoCa"
  239.  
  240. ENDPROC
  241.  
  242. PROC end() OF locale
  243. /****** locale/end ******************************
  244.  
  245.     NAME
  246.         end() of locale -- Global destructor.
  247.  
  248.     SYNOPSIS
  249.         locale.end()
  250.  
  251.     FUNCTION
  252.         Closes open catalog and locale.library.
  253.  
  254.     SEE ALSO
  255.         locale
  256.  
  257. ********/
  258.  
  259.   self.closeCatalog(self.catalog)
  260.  
  261.   self.close()
  262.  
  263. ENDPROC
  264.  
  265. PROC select(optionlist, index) OF locale
  266. /****** locale/select ******************************
  267.  
  268.     NAME
  269.         select() of locale -- Selection of action upon initialisation.
  270.  
  271.     SYNOPSIS
  272.         locale.select(LONG, LONG)
  273.  
  274.         locale.select(optionlist, index)
  275.  
  276.     FUNCTION
  277.         The following tags are recognized:
  278.             "ctlg" -- Name of catalog to open. The catalog will be opened
  279.                 at once via openCatalog().
  280.             "lang" -- Sets the builtin language. Any string or NIL for
  281.                 'english'
  282.  
  283.     INPUTS
  284.         optionlist:LONG -- List of options.
  285.  
  286.         index:LONG -- Index of optionlist.
  287.  
  288.     SEE ALSO
  289.         locale
  290.  
  291. ********/
  292. DEF item
  293.  
  294.   item:=ListItem(optionlist,index)
  295.  
  296.   SELECT item
  297.  
  298.     CASE "ctlg" -> open catalog
  299.  
  300.       INC index
  301.       self.openCatalog(NIL, ListItem(optionlist,index), self.builtinLanguage)
  302.  
  303.     CASE "lang" -> set builtin language
  304.  
  305.       INC index
  306.       self.builtinLanguage := ListItem(optionlist,index)
  307.  
  308.   ENDSELECT
  309.  
  310. ENDPROC index
  311.  
  312. PROC getObjectString(object:PTR TO object, container:PTR TO string,number, default=NIL:PTR TO CHAR) OF locale
  313. /****** locale/getObjectString ******************************************
  314.  
  315.     NAME
  316.         getLocalizedObjectString() -- Get string from object catalog
  317.  
  318.     SYNOPSIS
  319.         locale.getObjectString(PTR TO object, PTR TO string, LONG, PTR TO CHAR=NIL:PTR TO CHAR)
  320.  
  321.         locale.getObjectString(object, container, number, default)
  322.  
  323.     FUNCTION
  324.         Gets a string from the object's catalog.
  325.  
  326.     INPUTS
  327.         object:PTR TO object -- Object to get the string for
  328.  
  329.         container:PTR TO string -- String object to store the string in
  330.  
  331.         number:LONG -- Number of string to get
  332.  
  333.         default=NIL:PTR TOF CHAR -- The default string to return if there is no string
  334.             of that number.
  335.  
  336.     RESULT
  337.         "NoCa" if the locale.library couldn't be opened.
  338.         The default string if there's no entry of that number in the catalog,
  339.         otherwise the requested string.
  340.  
  341.     NOTES
  342.         The returned string from the catalog is READ-ONLY.
  343.  
  344. ******************************************************************************/
  345.  
  346. DEF string:PTR TO string,
  347.     oldCatalog,
  348.     stringToReturn:PTR TO CHAR
  349.  
  350.   END container
  351.   NEW container.new()
  352.  
  353.   NEW string.new()
  354.   string.cat('oomodules/')
  355.   string.cat(object.name())
  356.   string.cat('.catalog')
  357.  
  358.  /*
  359.   * 'save' the old catalog
  360.   */
  361.  
  362.   oldCatalog := self.catalog
  363.  
  364.  /*
  365.   * Set it to NIL so self.openCatalog() won't close it
  366.   */
  367.  
  368.   self.catalog:=NIL
  369.  
  370.  /*
  371.   * Open the object's catalog
  372.   */
  373.  
  374.   self.openCatalog(NIL, string.write(), self.builtinLanguage)
  375.  
  376.   stringToReturn :=  self.getString(number,default)
  377.   container.cat(stringToReturn)
  378.  
  379.   self.closeCatalog(self.catalog)
  380.  
  381.  /*
  382.   * Restore the old catalog
  383.   */
  384.  
  385.   self.catalog := oldCatalog
  386.  
  387.   END string
  388.  
  389. ENDPROC
  390.  
  391.  
  392. PROC name() OF locale IS 'Locale'
  393.